from app.outdoor.models import ActivityType
from app.aoduo_admin.models import AdminOperationLog
from django.http import JsonResponse
from rest_framework.views import APIView
from common.dictInfo import model_to_dict
from common.authCheck.adminAuth import getAdminUser, check_admin_login, adminAuthCheck
import json


class AdminActivityTypeControlView(APIView):
    """
    id9: ADD Activity Type
    id10: DELETE Activity Type
    id11: VIEW Activity Type
    id12: MOD Activity Type
    """

    @check_admin_login
    def get(self, request, tpid):
        """
        获取活动类型详情
        :param request:
        :param tpid:
        :return:
        """
        admin_user = getAdminUser(request.session.get('admin'))
        authID = 11
        if adminAuthCheck(admin_user, authID):
            acType = ActivityType.objects.filter(id=tpid)
            if not acType.exists():
                return JsonResponse({
                    'status': False,
                    'errMsg': '类型不存在'
                }, status=404)
            acType = acType[0]
            acTypeDict = model_to_dict(acType)
            AdminOperationLog.objects.create(
                related_admin=admin_user,
                related_auth_type_id=authID,
                other_message='view activity type ' + str(tpid)
            )
            return JsonResponse({
                'status': True,
                'activity_type': acTypeDict
            })
        else:
            return JsonResponse({
                'status': False,
                'errMsg': '你没有权限操作'
            }, status=401)

    @check_admin_login
    def delete(self, request, tpid):
        """
        删除活动类型
        :param request:
        :param tpid:
        :return:
        """
        admin_user = getAdminUser(request.session.get('admin'))
        authID = 10
        if adminAuthCheck(admin_user, authID):
            acType = ActivityType.objects.filter(id=tpid)
            if not acType.exists():
                return JsonResponse({
                    'status': False,
                    'errMsg': '类型不存在'
                }, status=404)
            acType = acType[0]
            acType.delete()
            AdminOperationLog.objects.create(
                related_admin=admin_user,
                related_auth_type_id=authID,
                other_message='delete activity type ' + str(tpid)
            )
            return JsonResponse({
                'status': True,
                'id': tpid
            })
        else:
            return JsonResponse({
                'status': False,
                'errMsg': '你没有权限操作'
            }, status=401)

    @check_admin_login
    def put(self, request, tpid):
        """
        修改活动类型
        :param request:
        :param tpid:
        :return:
        """
        admin_user = getAdminUser(request.session.get('admin'))
        authID = 12
        if adminAuthCheck(admin_user, authID):
            acType = ActivityType.objects.filter(id=tpid)
            if not acType.exists():
                return JsonResponse({
                    'status': False,
                    'errMsg': '类型不存在'
                }, status=404)
            acType = acType[0]
            jsonParams = json.loads((request.body).decode('utf-8'))
            acType.name = jsonParams.get('name', acType.name)
            acType.description = jsonParams.get('description', acType.description)
            acTypeDict = model_to_dict(acType)
            AdminOperationLog.objects.create(
                related_admin=admin_user,
                related_auth_type_id=authID,
                other_message='mod activity type ' + str(tpid)
            )
            return JsonResponse({
                'status': True,
                'activity_type': acTypeDict
            })
        else:
            return JsonResponse({
                'status': False,
                'errMsg': '你没有权限操作'
            }, status=401)

    @check_admin_login
    def post(self, request):
        """
        新增活动类型
        :param request:
        :return:
        """
        admin_user = getAdminUser(request.session.get('admin'))
        authID = 9
        if adminAuthCheck(admin_user, authID):
            jsonParams = json.loads((request.body).decode('utf-8'))
            newType = ActivityType.objects.create(
                name=jsonParams.get('name'),
                description=jsonParams.get('description'),
                creator=admin_user
            )
            AdminOperationLog.objects.create(
                related_admin=admin_user,
                related_auth_type_id=authID,
                other_message='add activity type ' + str(newType.id)
            )
            return JsonResponse({
                'status': True,
                'id': newType.id,
                'name': newType.name
            })
        return JsonResponse({
            'status': False,
            'errMsg': '你没有权限操作'
        }, status=401)
